package com.ctrip.frame;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.go.commons.utils.StringUtils;

import javax.swing.table.AbstractTableModel;

public class OrderTableModel extends AbstractTableModel {
    private JSONArray data;
    private String[] tableColumns = {"订单号", "下单时间", "评分", "类型", "出发地", "目的地", "往返日期", "人均预算", "人数", "订单状态", "备注"};

    public OrderTableModel(JSONArray data) {
        this.data = data;
    }

    public OrderTableModel() {
    }

    @Override
    public String getColumnName(int column) {
        return tableColumns[column];
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }

    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public int getColumnCount() {
        return tableColumns.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if (data.size() > 0) {
            JSONObject order = data.getJSONObject(rowIndex);
            switch (columnIndex) {
                case 0:
                    return order.getString("OrderID");
                case 1:
                    return order.getString("OrderCreateTimeAlert");
                case 2:
                    return order.getString("OrderRank") + "分";
                case 3:
                    return order.getString("TravelNature");
                case 4:
                    return order.getString("Departure");
                case 5:
                    return order.getString("Destination");
                case 6:
                    return String.format("往 %s 返%s", order.getString("NewVersionMaxDepartureDate"), order.getString("NewVersionReturnDate"));
                case 7:
                    Double budget = order.getDouble("MinBudget");
                    if (budget != null && budget > 0) {
                        return budget.toString();
                    }
                    return "无";
                case 8:
                    Integer adultNum = order.getInteger("AdultNum");
                    Integer childNum = order.getInteger("ChildNum");
                    StringBuilder sb = new StringBuilder();
                    if (adultNum != null && adultNum > 0) {
                        sb.append(adultNum).append(" 成人");
                    }
                    if (childNum != null && childNum > 0) {
                        if (sb.length() > 0) {
                            sb.append(",");
                        }
                        sb.append(childNum).append(" 儿童");
                    }
                    return sb.toString();
                case 9:
                    return order.getString("OrderStatusName");
                case 10:
                    String remark = order.getString("Remark");
                    if (StringUtils.isEmpty(remark)) {
                        remark = "无";
                    }
                    return remark;

            }
        }
        return null;
    }

    public void setData(JSONArray data) {
        this.data = data;
        fireTableDataChanged();
    }
}
